package com.metal.silver.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.metal.silver.domain.Customer;
import com.metal.silver.service.ICustomerService;
import com.metal.silver.service.form.FormDataService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户Controller
 * 
 * @author ruoyi
 * @date 2022-10-14
 */
@Controller
@RequestMapping("/metal-silver/customer2")
public class Customer2Controller extends BaseController
{
    private String prefix = "metal-silver/customer";

    @Autowired
    private ICustomerService customerService;

    @RequiresPermissions("metal-silver:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询客户列表
     */
    @RequiresPermissions("metal-silver:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer)
    {
//        startPage();

        return FormDataService.list(customer);

//        List<Customer> list = customerService.selectCustomerList(customer);
//        return getDataTable(list);
    }

    /**
     * 导出客户列表
     */
    @RequiresPermissions("metal-silver:customer:export")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Customer customer)
    {
        List<Customer> list = customerService.selectCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        return util.exportExcel(list, "客户数据");
    }

    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @RequiresPermissions("metal-silver:customer:add")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Customer customer)
    {
        return toAjax(FormDataService.addSave(customer));
//        return toAjax(customerService.insertCustomer(customer));
    }

    /**
     * 修改客户
     */
    @RequiresPermissions("metal-silver:customer:edit")
    @GetMapping("/edit/{uuid}")
    public String edit(@PathVariable("uuid") String uuid, ModelMap mmap) throws ClassNotFoundException {
        String domainName = "customer";
        String fullName = "com.metal.silver.domain." + StrUtil.upperFirst(domainName);
        Class clazz = Class.forName(fullName);
        JSONObject customer = FormDataService.selectByUuid("customer", uuid);
//        Customer customer = customerService.selectCustomerByUuid(uuid);
        // 不能直接传hashmap类型，否则报错：
        // kelvin todo: org.springframework.beans.NotReadablePropertyException: Invalid property 'uuid' of bean class [java.util.HashMap]: Bean property 'uuid' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
        mmap.put("customer", JSONObject.toJavaObject(customer, clazz));
        return prefix + "/edit";
    }

    /**
     * 修改保存客户
     */
    @RequiresPermissions("metal-silver:customer:edit")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Customer customer)
    {
        return toAjax(FormDataService.editSave( customer));
//        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 删除客户
     */
    @RequiresPermissions("metal-silver:customer:remove")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(FormDataService.deleteByUuids(ids));
//        return toAjax(customerService.deleteCustomerByUuids(ids));
    }
}
